package com.lixingqu.mvpdemo.ui.news;

import com.lixingqu.mvpdemo.entity.NewsEntity;
import com.lixingqu.mvpdemo.net.HttpResultFunc;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by lixingqu on 2016/12/2.
 */

public class NewsPresenterImpl implements NewsContract.Presenter {
    private CompositeSubscription mSubscriptions;
    private NewsContract.View mView;
    private NewsContract.Model mModel;

    public NewsPresenterImpl(NewsContract.View mView, NewsContract.Model mModel) {
        this.mView = mView;
        this.mModel = mModel;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }

    @Override
    public void onRefresh() {
        mModel.refresh()
                .map(new HttpResultFunc<List<NewsEntity>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<NewsEntity>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        mView.onRequestStart();
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onRequestError();
                    }

                    @Override
                    public void onNext(List<NewsEntity> newsEntities) {
                        mView.refresh(newsEntities);
                    }
                });
    }

    @Override
    public void onLoadMore() {
        mModel.loadMore()
                .map(new HttpResultFunc<List<NewsEntity>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<NewsEntity>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        mView.onRequestStart();
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onRequestError();
                    }

                    @Override
                    public void onNext(List<NewsEntity> newsEntities) {
                        mView.loadMore(newsEntities);
                    }
                });
    }

    @Override
    public void subscribe() {
//        Subscription subscription = mModel.getNews()
//                .map(new HttpResultFunc<List<NewsEntity>>())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<List<NewsEntity>>() {
//                    @Override
//                    public void onStart() {
//                        super.onStart();
//                        mView.onRequestStart();
//                    }
//
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        mView.onRequestError();
//                    }
//
//                    @Override
//                    public void onNext(List<NewsEntity> newsEntities) {
//                        mView.refresh(newsEntities);
//                    }
//                });
//        mSubscriptions.add(subscription);
    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }
}
